SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE PROC [dbo].[SP_TTANTC_ANUL_D01]
/*-------------------------------------------------------*/
/*---Empresa              : OFISIS S.A.                --*/
/*---Cliente              : OFISIS                 --*/
/*---Sistema              : Tesoreria                  --*/
/*---M÷dulo               : Empresa                    --*/
/*---Programa             : Anulaci¸n Aplicaci¸n       --*/
/*---                       Notas Credito con Facturas --*/
/*---Script               : SP_TTANTC_ANUL_D01.SQL--*/
/*---Nombre SP            : SP_TTANTC_ANUL_D01    --*/
/*---Desarrollado por     : Eduardo Vivar Pomiano      --*/
/*---Fecha Creaci÷n       : 25/02/2000                 --*/
/*---Base Datos           : Microsoft Sql Server       --*/
/*---Versi÷n              : 7.0                        --*/
/*---Invoca a SP          :                            --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Modificado 1 por     :                            --*/
/*---Fecha Modificaci÷n   :                            --*/ 
/*---Detalle Modificaci÷n :                            --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Comentarios                                       --*/
/*---                                                  --*/
/*---                                                  --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Drop Proc SP_TTANTC_ANUL_D01                 --*/
/*---Grant all on SP_TTANTC_ANUL_D01 to public    --*/ 
/*---SP_TTANTC_ANUL_D01   '99','sa', '00000001','10012198','900','01/02/2000'                  --*/
/*-------------------------------------------------------*/
 @ISCO_EMPR       TD_VC_002, 
 @ISCO_USUA_MODI  TD_VC_008,
 @ISNU_AMAR       TD_VC_008, 
 @ISCO_TIPO_OPER  TD_VC_003,
 @ISCO_UNID_CONC  TD_VC_003,
 @IDFE_AMAR	  TD_DT_001
	As
DECLARE @CSTI_DOCU_DEST 	TD_VC_003, 
         @CSNU_DOCU_DEST 	TD_VC_020,  
         @CNIM_COBR_CDES 	TD_NU_016_004, 
	 @VNIM_TOTA_DOCU 	TD_NU_016_004, 
	 @CSCO_ENTI	 	TD_VC_020, 
         @CSTI_DOCU_ORIG 	TD_VC_003, 
         @CSNU_DOCU_ORIG 	TD_VC_020, 
         @CNIM_COBR_CORI 	TD_NU_016_004,  
         @VSCO_MONE      	TD_VC_003,
         @VSCO_MONE_DOCU 	TD_VC_003,
         @VNFA_TIPO_CAMB 	TD_NU_015_006,
	 @VSCO_MONE_DEFA 	TD_VC_003,
	 @VSCO_MONE_EXTR 	TD_VC_003,
	 @OSTI_DOCU_LETR 	TD_VC_003,
 	 @VSCO_TIPO_INAN        TD_VC_003,   
         @VSST_GEAS_ANUL        TD_VC_001,
         @VSCO_TIPO_OPE1        TD_VC_003

  /***  PRIMERO LIBERA DOCUMENTOS  DESTINOS  ***/  

 BEGIN TRAN SP_TTANTC_ANUL_D01

     EXEC SP_TTDOCU_CNTB_Q02 6, @OSTI_DOCU_LETR OUTPUT	     

	/* SE ANULAN LAS LETRAS */
     	DELETE TMLETR_CLIE
	FROM TMLETR_CLIE T1, TDANTC_LETR T2
     	WHERE T2.CO_EMPR = @ISCO_EMPR
	AND T2.CO_UNID_CONC = @ISCO_UNID_CONC
	AND T2.NU_AMAR  = @ISNU_AMAR 
	AND T2.FE_AMAR  = @IDFE_AMAR 
	AND T2.NU_DOCU_CLIE is NULL 
	AND T1.CO_EMPR = T2.CO_EMPR
	AND T1.CO_CLIE = T2.CO_ENTI
	AND T1.NU_LETR_CLIE = T2.NU_LETR_CLIE
   
	If @@error <> 0
           Begin
	     CLOSE CU_TTANTC_ANUL_D01
	     DEALLOCATE CU_TTANTC_ANUL_D01
             rollback tran 
             return
         end

	/* SE ANULAN LOS ANTICIPOS */
     	DELETE TMDOCU_CLIE
	FROM TMDOCU_CLIE T1, TDANTC_LETR T2
     	WHERE T2.CO_EMPR = @ISCO_EMPR
	AND T2.CO_UNID_CONC = @ISCO_UNID_CONC
	AND T2.NU_AMAR  = @ISNU_AMAR 
	AND T2.FE_AMAR  = @IDFE_AMAR 
	AND T2.NU_LETR_CLIE is NULL 
	AND T1.CO_EMPR = T2.CO_EMPR
	AND T1.CO_CLIE = T2.CO_ENTI
	AND T1.CO_TIPO_DOCU = T2.CO_TIPO_DOCU
	AND T1.NU_DOCU_CLIE = T2.NU_DOCU_CLIE
   
	If @@error <> 0
           Begin
	     CLOSE CU_TTANTC_ANUL_D01
	     DEALLOCATE CU_TTANTC_ANUL_D01
             rollback tran 
             return
         end






 DELETE TDANTC_LETR
 WHERE CO_EMPR = @ISCO_EMPR
 AND   CO_UNID_CONC = @ISCO_UNID_CONC
 AND   NU_AMAR = @ISNU_AMAR 
 AND    FE_AMAR  = @IDFE_AMAR

 If @@error <> 0
    Begin
       Rollback tran 
       Return
    End


 DELETE TCANTC_LETR
 WHERE CO_EMPR = @ISCO_EMPR
 AND   CO_UNID_CONC = @ISCO_UNID_CONC
 AND   NU_AMAR = @ISNU_AMAR 
 AND    FE_AMAR  = @IDFE_AMAR

 If @@error <> 0
    Begin
       Rollback tran 
       Return
    End


COMMIT TRAN 

/*------------------------ Fin ------------------------------*/


GO
